package com.yociyy.gen.biz;

import com.yociyy.gen.entity.GenColumn;
import com.yociyy.gen.entity.GenTable;
import com.yociyy.gen.mapper.GenColumnMapper;
import com.yociyy.gen.mapper.GenTableMapper;
import com.yociyy.gen.utils.GenUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成器 - 数据表
 * 
 * @author: YoCiyy
 * @date: 2020/4/28
 */
@Service
public class GenTableBiz {

	@Autowired
	private GenTableMapper genTableMapper;

	@Autowired
	private GenColumnMapper genColumnMapper;


	public List<GenTable> selectDBTableList(Map<String, Object> params) {
		return genTableMapper.selectDBTableList(params);
	}

	public byte[] genCode(String[] tableNames) {

		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);

		for (String tableName : tableNames) {
			// 查询表信息
			GenTable table = genTableMapper.selectTables(tableName);
			// 查询列信息
			List<GenColumn> columns = genColumnMapper.selectColumns(tableName);
			// 生成代码
			GenUtils.genCode(table, columns, zip);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();

	}
}
